package com.example.administrator.base.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.bean.ReLogout;
import com.example.administrator.base.R;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.InputMethodUtil;
import com.example.administrator.base.util.SharedPreferencesUtils;

import java.io.IOException;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class NewPhone extends BaseActivity {

    @InjectView(R.id.Toolbar)
    android.support.v7.widget.Toolbar Toolbar;
    @InjectView(R.id.et_phone)
    EditText etPhone;
    @InjectView(R.id.et_code)
    EditText etCode;
    @InjectView(R.id.btn_code)
    Button btnCode;
    @InjectView(R.id.btn_next)
    Button btnNext;
    SweetAlertDialog sweetAlertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_phone);
        ButterKnife.inject(this);
        sweetAlertDialog = new SweetAlertDialog(this, 5);
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setTitleText("加载中");
        etPhone.setFocusableInTouchMode(true);
        etPhone.requestFocus();
        InputMethodUtil.showInputMethod(etPhone, 998);
        Toolbar.setTitle("赳猎人");
        Toolbar.setSubtitle("修改手机号");
        setSupportActionBar(Toolbar);
        Toolbar.setNavigationIcon(R.drawable.backbar);
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    btnCode.setEnabled(true);
                } else {
                    btnCode.setEnabled(false);
                }
            }
        });
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    btnNext.setEnabled(true);
                } else {
                    btnNext.setEnabled(false);
                }
            }
        });
    }

    @Override
    protected void onResponseSuccess(Response response, String action) {
        ResponseBody body = response.body();
        switch (action) {
            case "GETVERIFYCODE":
                if (body != null) {
                    try {
                        String string = body.string();
                        final ReLogout mse = GsonTools.changeGsonToBean(string, ReLogout.class);
                        if (mse != null) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    showToast("" + mse.getMessage());
                                    sweetAlertDialog.dismiss();
                                }
                            });
                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    showToast("数据异常请重试");
                                    sweetAlertDialog.dismiss();
                                }
                            });

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            sweetAlertDialog.dismissWithAnimation();
                            showToast("数据不完整，请稍后再试");
                        }
                    });
                }
                break;

            case "VERIFICATIONNEWPHONECODE":
                if (body != null) {
                    try {
                        String string = body.string();
                        final ReLogout mse = GsonTools.changeGsonToBean(string, ReLogout.class);
                        if (mse != null) {
                            if (mse.getCode() == 1) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        sweetAlertDialog.dismiss();
                                        showToast("" + mse.getMessage());
                                        finish();
                                    }
                                });
                            } else {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        sweetAlertDialog.dismiss();
                                        showToast("" + mse.getMessage());
                                    }
                                });
                            }
                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    sweetAlertDialog.dismiss();
                                    showToast("数据异常请重试");

                                }
                            });
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            sweetAlertDialog.dismissWithAnimation();
                            showToast("数据不完整，请稍后再试");
                        }
                    });
                }

                break;
        }

    }

    @Override
    protected void onResponseFailure(Throwable e, String action) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sweetAlertDialog.dismissWithAnimation();
                showToast("网络异常，请稍后再试");
            }
        });
    }

    @OnClick({R.id.btn_code, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_code:
                sweetAlertDialog.show();
                String phone = etPhone.getText().toString();
                getYzm(phone);
                break;
            case R.id.btn_next:
                sweetAlertDialog.show();
                String phonex = etPhone.getText().toString();
                String code = etCode.getText().toString();
                RephoneAndcode(phonex, code);
                break;
        }
    }

    /*获取验证码*/
    public void getYzm(String phone) {
        RequestBody formBody = new FormBody.Builder()
                .add("action", "GETVERIFYCODE")
                .add("phone", phone)
                .build();

        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/system?")
                .post(formBody)
                .build();
        postRequest(request, "GETVERIFYCODE");

    }

    /*验证验证码*/
    public void RephoneAndcode(String phone, String code) {
        String mAdminId = String.valueOf(SharedPreferencesUtils.getParam(NewPhone.this, "adminId", ""));
        RequestBody formBody = new FormBody.Builder()
                .add("action", "VERIFICATIONNEWPHONECODE")
                .add("adminId",mAdminId)
                .add("NewPhone", phone)
                .add("code", code)
                .build();

        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/administrator?")
                .post(formBody)
                .build();

        postRequest(request, "VERIFICATIONNEWPHONECODE");

    }
}
